include("shared.lua")
local carPos
function ENT:Initialize()
end

function ENT:Draw()
	self:DrawModel()
end

function ENT:Think()
end

function ChangeColor(color)
	net.Start("paintJobSwitch")
		net.WriteString(tostring(color.r))
		net.WriteString(tostring(color.g))
		net.WriteString(tostring(color.b))
	net.SendToServer()
end

function carMenu()
	//PrintTable(list.Get("Vehicles"))
local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself
DermaPanel:SetSize( 560, 400 ) -- Size of the frame
DermaPanel:SetPos( ScrW()/2 - DermaPanel:GetWide()/2, ScrH()/2 - DermaPanel:GetTall()/2 ) -- Position on the players screen
DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( false ) -- Draggable by mouse?
DermaPanel:ShowCloseButton( true ) -- Show the close button?
DermaPanel:MakePopup() -- Show the frame

local PropertySheet = vgui.Create( "DPropertySheet" )
PropertySheet:SetParent( DermaPanel )
PropertySheet:SetPos( 5, 30 )
PropertySheet:SetSize( DermaPanel:GetWide()-10, DermaPanel:GetTall()-35 )
 
	local SheetItemOne = vgui.Create( "DFrame" ) -- Creates the frame itself
	SheetItemOne:SetSize( 560, 400 ) -- Size of the frame
	SheetItemOne:SetPos( ScrW()/2 - DermaPanel:GetWide()/2, ScrH()/2 - DermaPanel:GetTall()/2 ) -- Position on the players screen
	SheetItemOne:SetTitle( "" ) -- Title of the frame
	SheetItemOne:SetVisible( true )
	SheetItemOne:SetDraggable( false )
	SheetItemOne:ShowCloseButton( false )

		local colorCircle = vgui.Create( "DColorMixer" )

		carPickupIcons = vgui.Create( "DPanelList", SheetItemOne )
		carPickupIcons:EnableVerticalScrollbar( true ) 
		carPickupIcons:EnableHorizontal( true ) 
		carPickupIcons:SetPadding( 4 ) 
		carPickupIcons:SetPos(10,10)
		carPickupIcons:SetSize(SheetItemOne:GetWide()/2-20, SheetItemOne:GetTall()-20)

		if LocalPlayer():GetTable().OwnedCars then
			for k,v in pairs(LocalPlayer():GetTable().OwnedCars) do
				LocalPlayer():GetTable().CarsOut = LocalPlayer():GetTable().CarsOut or {}
				local icon = vgui.Create( "SpawnIcon", carPickupIcons ) 
				icon:SetModel( carSets[v].Model )
				icon:SetTooltip(v)
				if not table.HasValue(LocalPlayer():GetTable().CarsOut, v) then
			 		carPickupIcons:AddItem( icon )
			 	end
				icon.DoClick = function( icon )

					ChangeColor(colorCircle:GetColor())

					surface.PlaySound( "ui/buttonclickrelease.wav" )
					if table.HasValue(LocalPlayer():GetTable().OwnedCars, v) then
						net.Start("attemptPickup")
							net.WriteString(v)
						net.SendToServer()
						LocalPlayer():GetTable().CarsOut = LocalPlayer():GetTable().CarsOut or {}
						//table.insert(LocalPlayer():GetTable().CarsOut, v)
					else
						LocalPlayer():ChatPrint("Guess you think you're clever?")
					end

					icon:Remove()
					
				end 
			end
		end

		local warningLabel = vgui.Create( "DLabel", SheetItemOne ) -- Creates the frame itself
		warningLabel:SetText("WARNING: If you do not park your car in the garage before you leave town, \n                                    it will not be saved as yours! ")
		warningLabel:SetFont("ChatFont")
		warningLabel:SizeToContents()
		warningLabel:SetTextColor(Color(160,0,0,255))
		warningLabel:SetPos( SheetItemOne:GetWide()/2 - warningLabel:GetWide()/2-7, SheetItemOne:GetTall()-100 )
	

		colorCircle:SetParent(SheetItemOne)
		//colorCircle:SetSize( 193, 193 )
		colorCircle:SetPos( SheetItemOne:GetWide()-30-colorCircle:GetWide(), SheetItemOne:GetTall()-110-colorCircle:GetTall() )

	local SheetItemTwo = vgui.Create( "DFrame" ) -- Creates the frame itself
	SheetItemTwo:SetSize( 560, 400 ) -- Size of the frame
	SheetItemTwo:SetPos( ScrW()/2 - DermaPanel:GetWide()/2, ScrH()/2 - DermaPanel:GetTall()/2 ) -- Position on the players screen
	SheetItemTwo:SetTitle( "" ) -- Title of the frame
	SheetItemTwo:SetVisible( true )
	SheetItemTwo:SetDraggable( false )
	SheetItemTwo:ShowCloseButton( false ) 
	 
		local IconList = vgui.Create( "DPanelList", SheetItemTwo )
		IconList:EnableVerticalScrollbar( true ) 
		IconList:EnableHorizontal( true ) 
		IconList:SetPadding( 4 ) 
		IconList:SetPos(10,10)
		IconList:SetSize(SheetItemTwo:GetWide()-20, SheetItemTwo:GetTall()-20)

			for k,v in pairs(carSets) do
				local icon = vgui.Create( "SpawnIcon", IconList ) 
				icon:SetModel( v.Model )
				icon:SetTooltip(k .. "\n        $" .. v.Price)
			 	IconList:AddItem( icon )
				icon.DoClick = function( icon ) 
					surface.PlaySound( "ui/buttonclickrelease.wav" )
					if tonumber(LocalPlayer().DarkRPVars.money)>=v.Price then
						net.Start("buyCar")
							net.WriteString(k)
						net.SendToServer()

						net.Start("attemptPickup")
							net.WriteString(k)
						net.SendToServer()
					else
						GAMEMODE:AddNotify("You don't enough money for a " .. k , NOTIFY_ERROR, 5);
					end
				end 
			end





	PropertySheet:AddSheet( "Pickup your car", SheetItemOne, "gui/silkicons/group", false, false, "Can I haz meh cheezburger now?" )
	PropertySheet:AddSheet( "Buy a car", SheetItemTwo, "gui/silkicons/user", false, false, "WOW It's a text box!!!" ) 
end

net.Receive("startCarMenu", carMenu)

function updateGarage()
	LocalPlayer():GetTable().OwnedCars = net.ReadTable()
end
net.Receive("updateGarage", updateGarage)
